﻿$(function () {
    switch ($('#OrderState').val()) {
        case '1':
            $('#addbtn').html('进入审核流程');
            switch ($('#ModuleID').val()) {
                case '1':
                    if (isAuthority('169')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    if (isAuthority('168')) {
                        $('#editBtn').show();
                        $('#deleteBtn').show();
                    }
                    else {
                        $('#editBtn').hide();
                        $('#deleteBtn').hide();
                    }
                    break
                case '2':
                    if (isAuthority('206')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    if (isAuthority('205')) {
                        $('#editBtn').show();
                        $('#deleteBtn').show();
                    }
                    else {
                        $('#editBtn').hide();
                        $('#deleteBtn').hide();
                    }
                    break
                case '3':
                    if (isAuthority('243')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    if (isAuthority('242')) {
                        $('#editBtn').show();
                        $('#deleteBtn').show();
                    }
                    else {
                        $('#editBtn').hide();
                        $('#deleteBtn').hide();
                    }

                    break
                case '4':
                    if (isAuthority('283')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    if (isAuthority('282')) {
                        $('#editBtn').show();
                        $('#deleteBtn').show();
                    }
                    else {
                        $('#editBtn').hide();
                        $('#deleteBtn').hide();
                    }

                    break
                case '5':
                    $('#addbtn').hide();
                    break;
                default:

            }

            break;
        case '2':
            $('#addbtn').html('财务审核');
            $('#editBtn').hide();
            $('#deleteBtn').hide();
            switch ($('#ModuleID').val()) {
                case '1':
                    if (isAuthority('170')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '2':
                    if (isAuthority('207')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '3':
                    if (isAuthority('244')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '4':
                    if (isAuthority('284')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '5':
                    $('#addbtn').hide();
                    break;
                default:

            }
            break;
        case '3':
            $('#addbtn').html('渠道审核');
            $('#editBtn').hide();
            $('#deleteBtn').hide();
            switch ($('#ModuleID').val()) {
                case '1':
                    if (isAuthority('171')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '2':
                    if (isAuthority('208')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '3':
                    if (isAuthority('245')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '4':
                    if (isAuthority('285')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '5':
                    $('#addbtn').hide();
                    break;
                default:

            }
            break;
        case '4':
            $('#addbtn').html('管理审核');
            $('#editBtn').hide();
            $('#deleteBtn').hide();
            switch ($('#ModuleID').val()) {
                case '1':
                    if (isAuthority('172')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '2':
                    if (isAuthority('209')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '3':
                    if (isAuthority('246')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '4':
                    if (isAuthority('288')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '5':
                    $('#addbtn').hide();
                    break;
                default:

            }
            break;
        case '5':
            $('#addbtn').html('出款');
            $('#editBtn').hide();
            $('#deleteBtn').hide();
            switch ($('#ModuleID').val()) {
                case '1':
                    if (isAuthority('173')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '2':
                    if (isAuthority('210')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '3':
                    if (isAuthority('247')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '4':
                    if (isAuthority('287')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '5':
                    $('#addbtn').hide();
                    break;
                default:

            }
            break;
        case '6':
            $('#addbtn').html('核算');
            $('#editBtn').hide();
            $('#deleteBtn').hide();
            switch ($('#ModuleID').val()) {
                case '1':
                    if (isAuthority('174')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '2':
                    if (isAuthority('211')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '3':
                    if (isAuthority('248')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '4':
                    if (isAuthority('288')) {
                        $('#addbtn').show();
                    }
                    else {
                        $('#addbtn').hide();
                    }
                    break
                case '5':
                    $('#addbtn').hide();
                    break;
                default:

            }
            break;
        case '7':
            $('#addbtn').hide();
            $('#editBtn').hide();
            $('#deleteBtn').hide();
            break;
        default:

    }
    initTable();
});
var $table = $('#table');
function initTable() {
    $table.bootstrapTable({
        url: '/Account/OutMoneyOrders/OutMoneyOrdersRunningsListAsync',            //数据来源地址
        method: 'post',                     //数据请求方式
        striped: true,                      //是否显示行间隔色
        cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
        pagination: true,                   //是否启用分页
        sidePagination: 'server',           //在服务器端分页
        queryParams: queryParams,           //传递参数
        pageNumber: 1,                      //初始化加载第一页，默认第一页
        pageSize: 20,                       //每页的记录行数（*）
        pageList: [10, 20, 30, 40, 50],     //可供选择的每页的行数（*）
        clickToSelect: true,                //是否启用点击选中行
        //toolbar: "#tableToolBar",
        height: getHeight(),
        search: false,
        showRefresh: true,
        showColumns: false,
        showExport: true,                     //是否显示导出
        exportDataType: "basic",              //basic', 'all', 'selected'.
        exportTypes: ['excel'],
        exportOptions: { fileName: '123' },
        toolbar: '#exampleToolbar',
        iconSize: 'outline',
        icons: {
            refresh: 'glyphicon-repeat',
            columns: 'glyphicon-list'
        },
        toolbar: '#exampleToolbar',
        columns: [{
            width: 38,
            field: 'aaaaaaa',
            checkbox: true,
        }, {
            field: 'runningsID',
            title: '流水编号',
            align: 'center',
        }, {
            field: 'itemName',
            title: '项目名称',
            align: 'center',
        }, {
            field: 'itemPrice',
            title: '成交价',
            align: 'center',
        }, {
            field: 'customerName',
            title: '客户姓名',
            align: 'center',
        }, {
            field: 'customerPhone',
            title: '客户电话',
            align: 'center',
        }, {
            field: 'itemTag',
            title: '地址门牌号',
            align: 'center',
        }, {
            field: 'itemTime',
            title: '认购时间',
            align: 'center',
        }, {
            field: 'intermediaryName',
            title: '中介名称',
            align: 'center',
        }, {
            field: 'accountBank',
            title: '开户行',
            align: 'center',
        }, {
            field: 'accountName',
            title: '开户名',
            align: 'center',
        }, {
            field: 'accountNumber',
            title: '开户帐号',
            align: 'center',
        }, {
            field: 'brokerageTypeName',
            title: '佣金类型',
            align: 'center',
        }, {
            field: 'brokerageAmount',
            title: '佣金金额',
            align: 'center',
        }, {
            field: 'brokerageTax',
            title: '佣金税点',
            align: 'center',
        }, {
            field: 'brokerageSettledAmount',
            title: '佣金已结',
            align: 'center',
        }, {
            field: 'BrokerageNoAmount',
            title: '待结佣金',
            align: 'center',
            formatter: function (value, row, index) {

                return row.brokerageAmount - row.brokerageTax - row.brokerageSettledAmount;
            }
        }, {
            field: 'amount',
            title: '结算金额',
            align: 'center',
        }, {
            field: 'originStaffName',
            title: '渠道人员',
            align: 'center',
        }, {
            field: 'sellStaffName',
            title: '销售人员',
            align: 'center',
        }, {
            field: 'createStaffName',
            title: '添加人',
            align: 'center',
            visible: false,
        }, {
            field: 'financeStaffName',
            title: '财务审核人',
            align: 'center',
            visible: false,
        }, {
            field: 'originStaffName',
            title: '渠道审核人',
            align: 'center',
            visible: false,
        }, {
            field: 'sysStaffName',
            title: '管理审核人',
            align: 'center',
            visible: false,
        }, {
            field: 'state',
            title: '状态',
            align: 'center',
            formatter: function (value, row, index) {
                
                return GetOutMoneyOrdersRunningsStateRemark(row.financeState,row.financeReamrk,row.originState,row.originRemark,row.sysState,row.sysRemark,row.outMoneyState,row.outMoneyReamrk);
            }
        }]
    });
}



//查询的参数
var queryParams = function (params) {
    var temp = {
        pageSize: params.limit,                             //行数
        pageIndex: (params.offset / params.limit) + 1,      //页码
        ID: $('#ID').val(),
    };
    console.log(temp);
    console.log(params);
    return temp;
};



//插入按钮
$('#addbtn').click(function (e) {
    switch ($('#OrderState').val()) {
        case '1':
            layer.confirm('您确定要进入审核流程吗？', {
                btn: ['是的', '取消'] //按钮
            }, function () {
                $.ajax({
                    url: "/Account/OutMoneyOrders/OutMoneyOrdersNextAsync",
                    type: "post",
                    dataType: "json",
                    data: { OutMoneyOrdersID: $('#ID').val() },
                    success: function (data) {
                        //Info,Success,Warning,Error
                        var type = data.type;
                        if (type == 1) {
                            layer.msg(data.content,
                                {
                                    icon: 6, time: 2000
                                },
                                function () {
                                    $('#OrderState').val('2');
                                    $('#addbtn').html('财务审核');
                                    refreshTable();
                                });
                        } else {
                            layer.msg(data.content, { icon: 5, time: 2000 });
                        }
                    }
                });
            }, function () {
                return;
            });
            break;
        case '2':
        case '3':
        case '4':
            var selected = $table.bootstrapTable('getSelections');
            if (selected.length == 0) {
                layer.msg("请选择您要审核的数据！", { icon: 5, time: 2000 });
                return;
            }

            var arr = [];
            for (var i = 0; i < selected.length; i++) {
                arr.push(selected[i].id);
            }

            var ids = arr.join(",");

            parent.layer.open({
                type: 2,
                title: '审核出款单',
                shadeClose: false,
                shade: 0.4,
                area: ['500px', '350px'],
                content: '/Account/OutMoneyOrders/OutMoneyOrdersAuditing?ID=' + $('#ID').val() + '&IDs=' + ids,
                end: function () {
                    location.reload();
                    //refreshTable();
                }
            });
            break;
        case '5':
            var selected = $table.bootstrapTable('getSelections');
            if (selected.length == 0) {
                layer.msg("请选择您要出款的数据！", { icon: 5, time: 2000 });
                return;
            }

            var arr = [];
            for (var i = 0; i < selected.length; i++) {
                arr.push(selected[i].id);
            }

            var ids = arr.join(",");

            parent.layer.open({
                type: 2,
                title: '出款',
                shadeClose: false,
                shade: 0.4,
                area: ['500px', '350px'],
                content: '/Account/OutMoneyOrders/OutMoneyOrdersOutMoney?ID=' + $('#ID').val() + '&IDs=' + ids,
                end: function () {
                    location.reload();
                    //refreshTable();
                }
            });
            break;
        case '6':
             layer.confirm('您确定审算吗？', {
                btn: ['是的', '取消'] //按钮
            }, function () {
                $.ajax({
                    url: "/Account/OutMoneyOrders/OutMoneyOrdersAdjustAsync",
                    type: "post",
                    dataType: "json",
                    data: { OutMoneyOrdersID: $('#ID').val() },
                    success: function (data) {
                        //Info,Success,Warning,Error
                        var type = data.type;
                        if (type == 1) {
                            layer.msg(data.content,
                                {
                                    icon: 6, time: 2000
                                },
                                function () {
                                    $('#addbtn').hide();
                                    refreshTable();
                                });
                        } else {
                            layer.msg(data.content, { icon: 5, time: 2000 });
                        }
                    }
                });
            }, function () {
                return;
            });
            break;
        default:

    }
});

$('#editBtn').click(function (e) {
    var selected = $table.bootstrapTable('getSelections');
    if (selected.length == 0) {
        layer.msg("请选择您要修改的数据！", { icon: 5, time: 2000 });
        return;
    }

    var arr = [];
    for (var i = 0; i < selected.length; i++) {
        arr.push(selected[i].id);
    }

    var ids = arr.join(",");
    layer.prompt({ title: '请输入结算金额', formType: 0 }, function (text, index) {
            if (isNaN(text)) {
                layer.msg("请数入数字！", { icon: 5, time: 2000 });
                return;
            }

        $.ajax({
            url: "/Account/OutMoneyOrders/ModifyAmountAsync",
            type: "post",
            dataType: "json",
            data: {
                Amount: text,
                OutMoneyOrdersID: $('#ID').val(),
                IDs: ids,
            },
            success: function (data) {
                //Info,Success,Warning,Error
                var type = data.type;
                if (type == 1) {
                    layer.msg(data.content, { icon: 6, time: 2000 });
                    refreshTable();
                } else {
                    layer.msg(data.content, { icon: 5, time: 2000 });
                }
            }
        });
        layer.close(index);
    });

})
$('#deleteBtn').click(function (e) {
    var selected = $table.bootstrapTable('getSelections');
    if (selected.length == 0) {
        layer.msg("请选择您要删除的数据！", { icon: 5, time: 2000 });
        return;
    }

    var arr = [];
    for (var i = 0; i < selected.length; i++) {
        arr.push(selected[i].id);
    }

    var ids = arr.join(",");
    $.ajax({
        url: "/Account/OutMoneyOrders/DeleteOutMoneyOrdersRunningsAsync",
        type: "post",
        dataType: "json",
        data: {
            OutMoneyOrdersID: $('#ID').val(),
            IDs: ids,
        },
        success: function (data) {
            //Info,Success,Warning,Error
            var type = data.type;
            if (type == 1) {
                layer.msg(data.content, { icon: 6, time: 2000 });
                refreshTable();
            } else {
                layer.msg(data.content, { icon: 5, time: 2000 });
            }
        }
    });

});
//获取表格高度
function getHeight() {
    return $(window).height() - 150;
};
//刷新表格数据
var refreshTable = function () {
    $table.bootstrapTable('refresh');
};

function GetOutMoneyOrdersRunningsStateRemark(FinanceState, FinanceReamrk, OriginState, OriginRemark, SysState, SysRemark, OutMoneyState, OutMoneyReamrk) {
    var s = "";
    if ($('#OrderState').val()=='1') {
        s = "";
    }else if (FinanceState==0)
    {
        s = '等待财务审核中';
    }
    else if (FinanceState==2)
    {
        s = '<span color="red">' + FinanceReamrk + '</span>';
    }
    else
    {
        if (OriginState == 0)
        {
            s = '等待渠道审核中';
        }
        else if (OriginState == 2)
        {
            s = '<span color="red">' + OriginRemark + '</span>';
        }
        else
        {
            if (SysState == 0)
            {
                s = "等待管理审核中";
            }
            else if (SysState == 2)
            {
                s = '<span color="red">' + SysRemark + '</span>';
            }
            else
            {
                if (OutMoneyState == 0)
                {
                    s = "出款中";
                }
                else if (OutMoneyState == 2)
                {
                    s = '<span color="red">' + OutMoneyReamrk + '</span>';
                }
                else
                {

                }
            }
        }
    }
    return s;
}